package com.ruicar.afs.cloud.channel.grade.vo;

import lombok.Data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @version 1.0
 * @description: 指标节点数
 * @projectName：afs-backend
 * @className：TreeNode
 * @see: com.ruicar.afs.cloud.channel.grade.vo
 * @author： jiangxiongfei
 * @createTime：2020-08-12 19:59
 */
@Data
public class TreeNode {
    public static final String CHECK_STATUS_CHECKED = "1";
    public static final String CHECK_STATUS_UNCHECKED = "0";
    public static final String CHECK_STATUS_INDETERMINATE = "2";

    //节点编号
    private String id = "";
    //节点内容
    private String text = "";
    //是否有孩子节点
    private boolean hasChild;
    //
    private String state = "closed";
    //能否选择
    private boolean canSelected;
    //上级节点
    private String pid = "";
    //图标
    private String iconCls = "";

    private Object attributes = "";
    //孩子节点集合
    private List<TreeNode> children = new ArrayList();
    //是否选择
    private boolean checked;
    //选择状态
    private String checkedStatus = "";


    public TreeNode() {
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return this.pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getText() {
        return this.text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getIconCls() {
        return this.iconCls;
    }

    public void setIconCls(String iconCls) {
        this.iconCls = iconCls;
    }

    public boolean isCanSelected() {
        return this.canSelected;
    }

    public void setCanSelected(boolean canSelected) {
        this.canSelected = canSelected;
    }

    public boolean isHasChild() {
        return this.hasChild;
    }

    public void setHasChild(boolean hasChild) {
        this.hasChild = hasChild;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public boolean isChecked() {
        return this.checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
        this.checkedStatus = "1";
    }

    public String getCheckedStatus() {
        return this.checkedStatus;
    }

    public void setCheckedStatus(String checkedStatus) {
        this.checkedStatus = checkedStatus;
    }

    public Object getAttributes() {
        return this.attributes;
    }

    public void setAttributes(Object attributes) {
        this.attributes = attributes;
    }

    public TreeNode appendTo(TreeNode node) {
        return node.addChild(this);
    }

    public TreeNode addChild(TreeNode node) {
        this.children.add(node);
        node.setPid(this.id);
        this.setHasChild(true);
        return this;
    }

    public TreeNode removeChild(TreeNode node) {
        this.children.remove(node);
        node.setPid((String) null);
        this.setHasChild(!this.children.isEmpty());
        return this;
    }

    public List<TreeNode> getChildren() {
        return this.children;
    }

    public static String toHTML(boolean checkboxs, int indent, List<TreeNode> nodes) {
        StringBuffer sb = new StringBuffer();
        Iterator i$ = nodes.iterator();

        while (i$.hasNext()) {
            TreeNode c = (TreeNode) i$.next();
            sb.append("<li id='" + c.id + "'>");
            sb.append(c.toHTML(indent, checkboxs));
            sb.append("</li>");
        }

        return sb.toString();
    }

    public String toHTML(boolean checkboxs, int indent) {
        StringBuffer sb = new StringBuffer();
        sb.append("<li id='" + this.id + "'>");
        sb.append(this.toHTML(indent, checkboxs));
        sb.append("</li>");
        return sb.toString();
    }

    private String toHTML(int deep, boolean checkboxs) {
        StringBuffer sb = new StringBuffer();
        sb.append("<div node-id=\"" + this.id + "\" class=\"tree-node\" >");

        for (int i = 0; i < deep; ++i) {
            sb.append("<span class=\"tree-indent\" />");
        }

        if (this.children.isEmpty() && !this.hasChild) {
            sb.append("<span class=\"tree-indent\" />");
        } else {
            sb.append("<span class=\"tree-hit ").append(!"closed".equals(this.state) && !this.hasChild ? "tree-expanded" : "tree-collapsed").append("\" />");
        }

        sb.append("<span class=\"tree-icon " + (this.children.isEmpty() && !this.hasChild ? "tree-file" : "tree-folder") + " ").append(!"closed".equals(this.state) && !this.hasChild ? "tree-folder" +
                "-open" : "").append(this.iconCls).append("\" />");
        if (checkboxs) {
            sb.append("<span class=\"tree-checkbox tree-checkbox" + ("".equals(this.checkedStatus) ? "0" : this.checkedStatus) + "\" />");
        }

        sb.append("<span class=\"tree-title\">").append(this.text).append("</span>");
        sb.append("</div>");
        if (!this.children.isEmpty()) {
            sb.append("<ul style=\"display: ").append("closed".equals(this.state) ? "none" : "block").append(";\" >");
            Iterator i$ = this.getChildren().iterator();

            while (i$.hasNext()) {
                TreeNode c = (TreeNode) i$.next();
                sb.append("<li id='" + c.id + "'>");
                sb.append(c.toHTML(deep + 1, checkboxs));
                sb.append("</li>");
            }

            sb.append("</ul>");
        }

        return sb.toString();
    }
}
